namespace SB2.Core
{
    public class PagedResponse
    {
        /// <summary>
        /// 总数
        /// </summary>
        /// <value></value>
        public int TotalCount { get; set; }
        /// <summary>
        /// 列信息
        /// </summary>
        /// <value></value>
        public List<DisplayColumnDto> Columns { get; set; }
    }
}
namespace SB2.Core
{
    public class PagedResponse<T> : PagedResponse
    {
        /// <summary>
        /// 数据明细
        /// </summary>
        /// <value></value>
        public List<T> List { get; set; }
        public PagedResponse(List<T> list, int totalCount)
        {
            this.List = list;
            this.TotalCount = totalCount;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="totalCount"></param>
        /// <param name="Columns"></param>
        public PagedResponse(List<T> list, int totalCount, List<DisplayColumnDto> Columns)
        {
            this.List = list;
            this.TotalCount = totalCount;
            this.Columns = Columns;
        }
        /// <summary>
        /// 根据列信息返回数据， 去掉列信息上没有的数据
        /// </summary>
        /// <returns></returns>
        public PagedResponse ToResponse()
        {
            if (Columns == null) return this;
            var properties = typeof(T).GetProperties()
            .ToDictionary(m => m.Name.ToLower(), m => m);
            var data = List.Select(m =>
            {
                return Columns.ToDictionary(p => p.ColumnName, p => properties.ContainsKey(p.ColumnName.ToLower()) ? properties[p.ColumnName.ToLower()].GetValue(m) : null);
            }).ToList();
            return new PagedResponse<Dictionary<string, object>>(data, TotalCount, Columns);
        }

    }
}